package GameMain.View.PTGame
{
	import GameMain.Game;
	import GameMain.Macro.MacroEvent;
	import GameMain.Macro.MacroSound;
	import GameMain.Model.Entity;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.utils.Dictionary;
	import flash.utils.getTimer;
	import flash.utils.setTimeout;
	
	import library.Function.GlobalFun;
	import library.SocketData.SocketData;
	
	
	public class CadenceCtrl
	{
		
		public var booleanNewPlayer:Boolean = false;
		
		private static var STEP_RIGHT:int = 0;
		private static var STEP_PRESS:int = 4;
		private static var STEP_NORNAL:int = 5
		private static var CADENCE_TIME:int = 500;			//节拍间隔
		private static var RIGHT_TIME:int = 200;				//按键正确时间
		private var _game:Sprite = null;
		private var _flashMusic:MovieClip = null;
		private var _startTime:int = 0;						//开始时间
		private var _pressList:Array = []; 					//按键列表
		private var _pressRightList:Array = [];				//按键正确率
		private var _key_Function:Object = {};
		private var _soundDict:Object = {};
		private var _soundEchoDict:Object = {};
		private var _key_labelDict:Object = {};
		private var _passStep:int = 0;
		private var _passBool:Boolean = false;
		
		private var _testTxt:TextField;
		
		private var btnList:Array = [];
		
		
		public function CadenceCtrl(game:Sprite,gameBack:Sprite)
		{
			this._game = game;
			this._flashMusic = gameBack["DA_JI_MC"] as MovieClip;
			gameBack["BTN_1"].visible = false;
			gameBack["BTN_2"].visible = false;
			gameBack["BTN_3"].visible = false;
			gameBack["BTN_4"].visible = false;
			this.btnList.push(gameBack["BTN_1"]);
			this.btnList.push(gameBack["BTN_2"]);
			this.btnList.push(gameBack["BTN_3"]);
			this.btnList.push(gameBack["BTN_4"]);
			this._flashMusic.stop();
			this._key_Function["A,A,A,D"] = this.handleAAAD;
			this._key_Function["W,A,W,A"] = this.handleWAWA;
			this._key_Function["D,D,A,D"] = this.handleDDAD;
			this._key_Function["W,W,A,D"] = this.handleWWAD;
			this._key_Function["D,D,W,W"] = this.handleDDWW;
			this._key_Function["D,A,D,A"] = this.handleDADA;
			this._key_Function["S,S,W,W"] = this.handleSSWW;
			this._key_Function["A,D,S,W"] = this.handleADSW;
			
			this._soundDict["W"] = MacroSound.SOUND_CHAKA;
			this._soundDict["A"] = MacroSound.SOUND_PATA;
			this._soundDict["S"] = MacroSound.SOUND_DON;
			this._soundDict["D"] = MacroSound.SOUND_PON;
			
			this._soundEchoDict["W"] = MacroSound.SOUND_CHAKA_ECHO;
			this._soundEchoDict["A"] = MacroSound.SOUND_PATA_ECHO;
			this._soundEchoDict["S"] = MacroSound.SOUND_DON_ECHO;
			this._soundEchoDict["D"] = MacroSound.SOUND_PON_ECHO;
			
			
			this._key_labelDict["W"] = "waka";
			this._key_labelDict["A"] = "bongo";
			this._key_labelDict["S"] = "chaka";
			this._key_labelDict["D"] = "boom";
			
			this.resetPressList();
		}
		
		private function initTestTxt():void
		{
			this._testTxt = new TextField;
			this._testTxt.multiline = false;
			this._testTxt.wordWrap = false;
			this._testTxt.x = 30;
			this._testTxt.y = 50;
			this._testTxt.width = 300;
			this._testTxt.selectable = false;
			GlobalFun.setTextFormat(this._testTxt,"SimSun",20,true,0xffffff);
			this._game.addChild(this._testTxt);
			this._testTxt.text = this._pressList.toString();
			this._testTxt.visible
		}
		
		public function regestEvent():void
		{
			Game.EngineMgr.regestKeyDownEvent(("W").charCodeAt(),functionW);
			Game.EngineMgr.regestKeyDownEvent(38,functionW);
			Game.EngineMgr.regestKeyDownEvent(("S").charCodeAt(),functionS);
			Game.EngineMgr.regestKeyDownEvent(40,functionS);
			Game.EngineMgr.regestKeyDownEvent(("A").charCodeAt(),functionA);
			Game.EngineMgr.regestKeyDownEvent(37,functionA);
			Game.EngineMgr.regestKeyDownEvent(("D").charCodeAt(),functionD);
			Game.EngineMgr.regestKeyDownEvent(39,functionD);
			this._startTime = getTimer();
			this._game.addEventListener(Event.ENTER_FRAME,onEnterFrame);
			this._flashMusic.play();
		}
		
		public function destroyEvent():void
		{
			Game.EngineMgr.destroyKeyUpEvent(("W").charCodeAt());
			Game.EngineMgr.destroyKeyUpEvent(("S").charCodeAt());
			Game.EngineMgr.destroyKeyUpEvent(("A").charCodeAt());
			Game.EngineMgr.destroyKeyUpEvent(("D").charCodeAt());
			this._game.removeEventListener(Event.ENTER_FRAME,onEnterFrame);
			this._flashMusic.stop();
		}
		
		private function functionW():void {handKeyEvent("W");}
		private function functionS():void {handKeyEvent("S");}
		private function functionA():void {handKeyEvent("A");}
		private function functionD():void {handKeyEvent("D");}
		
		
		private function onEnterFrame(e:Event):void
		{
			var nowtime:int = getTimer()
			var currentTime:int = nowtime - this._startTime;
			if(currentTime >= CADENCE_TIME)
			{
				//this._startTime = this._startTime + CADENCE_TIME;
				this._startTime = nowtime;
				if(this._passStep == STEP_NORNAL)
				{
					this.mistake("have press last time");
				}
				else if(this._passStep == STEP_PRESS)
				{
					this._passStep = STEP_NORNAL;
				}
				else
				{
					this.playPassStep();
				}
			}
		}
		
		private function playPassStep():void
		{
			var step:int = this._passStep;
			this.btnList[step].visible = false;
			Game.soundMgr.soundPlay(this._soundEchoDict[this._pressList[step]]);
			this._passStep++;
			if(this._passStep == STEP_PRESS)
			{
				this.resetPressList();
			}
		}
		
		
		private function handKeyEvent(key:String):void
		{
			if(this._passStep == STEP_PRESS)
			{
				
				//this.mistake("press twice in one time");
				//return;
			}
			else if(this._passStep != STEP_NORNAL)
			{
				return;
			}
				
			this._passStep = STEP_PRESS;
			
			var pressKeyTime:int = getTimer() - this._startTime;
			
			//Game.EngineMgr.printLog("pressKeyTime:",pressKeyTime);
			
			if( pressKeyTime < RIGHT_TIME)
			{
				this.pressRight(key,1);
			}
			else
			{
				this.pressRight(key,0.5);
			}
		}
		
		private function mistake(error:String = ""):void
		{
			//Game.EngineMgr.printLog(error);
			this.resetPressList();
			for(var i:int = 0;i<4;i++)
			{
				this.btnList[i].visible = false;
			}
		}
		
		private function resetPressList():void
		{
			this._pressList = [];
			this._pressRightList = [];
		}
		
		private function pressRight(key:String,_alpha:Number):void
		{
			this._pressList.push(key);
			this._pressRightList.push(_alpha);
			Game.soundMgr.soundPlay(this._soundDict[key])
			var len:int = this._pressList.length;
			var mc:MovieClip = this.btnList[len - 1] as MovieClip;
			mc.visible = true;
			mc.alpha = _alpha;
			mc.gotoAndStop(this._key_labelDict[key]);
			mc["drum"].gotoAndPlay(1);
			if(len == 4)
				checkHandleState();
		}
		
		private function checkHandleState():void
		{
			var keyStr:String = this._pressList.toString();
			if (this._key_Function.hasOwnProperty(keyStr))
			{
				this._passStep = STEP_RIGHT;
				this._key_Function[keyStr]();
			}
			else
			{
				this.mistake("No key value");
			}
		}
		
		private function handleAAAD():void
		{
			this.setAction(Entity.ACTION_MOVE_FORWARD);
		}
		
		private function handleWAWA():void
		{
			this.setAction(Entity.ACTION_MOVE_BACK);
		}
		
		private function handleDDAD():void
		{
			this.setAction(Entity.ACTION_NORMAL_ATTACK);
		}
		
		private function handleWWAD():void
		{
			
		}
		
		private function handleDDWW():void
		{
			
		}
		
		private function handleDADA():void
		{
			
		}
		
		private function handleSSWW():void
		{
			this.setAction(Entity.ACTION_JUMP);
		}
		
		private function handleADSW():void
		{
			
		}
		
		
		private function setAction(action:int):void
		{
			if(this.booleanNewPlayer == false)
			{
				sendActionToServer(action);
			}
			else
			{
				sendActionToClient(action);
			}
		}
		
		private function sendActionToServer(action:int):void
		{

			var data:SocketData = new SocketData();
			data.writeInt8(action);
			Game.EngineMgr.sendMessageData(MacroEvent.MSG_TO_SERVER_ACTION,data);
		}
		
		private function sendActionToClient(action:int):void
		{
			Game.EngineMgr.throwEvent(MacroEvent.MSG_CLIENT_TO_CLIENT,action);
		}
		
	}
}